﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Collections;
//using Bespoke.Common;
//using Bespoke.Common.Osc;
using System.IO;

namespace Loader
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        # region variables
        static ServiceReference2.patient[] ownPatient1;
        static ServiceReference2.patient[] othPatient1;
        static List<patient> ownPatient = new List<patient>();
        static List<patient> othPatient = new List<patient>();
        public static string docname = "";
        #endregion

        // to be called from MainWinow.xaml
        public void startWork(string doctor)
        {
            if (othPatient1 == null || !docname.Equals(doctor))
            {
                
                    ownPatient1 = null;
                    othPatient1 = null;
                    //ownPatient = null;
                    //othPatient = null;
                    ownPatient.Clear();
                    othPatient.Clear();
                    docname = doctor;
                ServiceReference2.Service1Client svc = new ServiceReference2.Service1Client();
                ownPatient1 = svc.retrieveAllOwnPatients(docname);
                othPatient1 = svc.retrieveAllOthPatients(docname);

                for (int i = 0; i < ownPatient1.Count(); i++)
                {
                    ServiceReference2.patient p1 = ownPatient1[i];
                    patient p = new patient(p1.Uid, p1.LastName, p1.FirstName, p1.Age, p1.ContactNum, p1.Address1, p1.Address2, p1.Postcode, p1.Doc, p1.E_name, p1.E_relationship, p1.E_contactNum, p1.ImagePath,p1.DateOfBirth,p1.Email,p1.Gender,p1.ContactNumHome,p1.Title,p1.Identifier,p1.ContactNumHome,p1.Country);
                    ownPatient.Add(p);
                }
                for (int i = 0; i < othPatient1.Count(); i++)
                {
                    ServiceReference2.patient p1 = othPatient1[i];
                    patient p = new patient(p1.Uid, p1.LastName, p1.FirstName, p1.Age, p1.ContactNum, p1.Address1, p1.Address2, p1.Postcode, p1.Doc, p1.E_name, p1.E_relationship, p1.E_contactNum, p1.ImagePath, p1.DateOfBirth, p1.Email, p1.Gender, p1.ContactNumHome, p1.Title, p1.Identifier, p1.ContactNumHome, p1.Country); 
                    othPatient.Add(p);
                }

                svc.Close();
            }

        }

        public Window1(string doctor)
        {
            InitializeComponent();
            docname = doctor;

        }

        public Window1()
        {
            InitializeComponent();
            lblDocname.Content = docname;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
            try
            {
                lblDocname.Content = docname;

                foreach (patient i in ownPatient)
            {
                //ServiceReference2.Service1Client svc = new ServiceReference2.Service1Client();
                //byte[] imagebyte = svc.retrieveImage(i.ImagePath[0]);
                //BitmapImage bi = new BitmapImage();
                //bi.BeginInit();
                //bi.StreamSource = new MemoryStream(imagebyte);
                //bi.EndInit();
                //i.Image = bi;
                //svc.Close();
                i.Image = new BitmapImage(new Uri("./Untitled.jpg", UriKind.Relative));
            }

            foreach (patient i in othPatient)
            {
                //ServiceReference2.Service1Client svc = new ServiceReference2.Service1Client();
                //byte[] imagebyte = svc.retrieveImage(i.ImagePath[0]);
                //BitmapImage bi = new BitmapImage();
                //bi.BeginInit();
                //bi.StreamSource = new MemoryStream(imagebyte);
                //bi.EndInit();
                //i.Image = bi;
                //svc.Close();
                i.Image = new BitmapImage(new Uri("./Untitled.jpg", UriKind.Relative));
            }
           
            lbx_ownPatient.ItemsSource = ownPatient;
            lbx_otherPatient.ItemsSource = othPatient;
            }
            catch (Exception) { }
            

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            patient p = new patient();
            try
            {
                if (tabControl1.SelectedIndex == 0)
                {
                    p = (patient)lbx_ownPatient.SelectedItem;
                }
                else if (tabControl1.SelectedIndex == 1)
                {
                    p = (patient)lbx_otherPatient.SelectedItem;
                }
                Window2 win = new Window2(p);
                win.fillupDoc(docname);
                win.Show();
                this.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Please select a patient");
            }
        }

        private void lbx_ownPatient_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Button_Click(btn_view, null);
        }

        private void lbx_otherPatient_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Button_Click(btn_view, null);
        }

        private void btn_logout_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            MainWindow win = new MainWindow();
            this.Hide();
            win.Show();
        }

    }
}

